package workerOutputMakers;

import static java.lang.System.err;
import static java.lang.System.exit;

import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import system.FileSystem;
import system.Worker;

import system.Part;
import system.Vertex;
import system.WorkerWriter;



/**
 *
 * @author Pete Cappello
 */
public class StandardWorkerOutputMaker implements WorkerWriter
{
    public void write( FileSystem fileSystem, Worker worker )
    {
        try
        {
            // open Worker file for output
            int workerNum = worker.getWorkerNum();
            FileOutputStream fileOutputStream = fileSystem.getWorkerOutputFileOutputStream( workerNum );
            DataOutputStream dataOutputStream = new DataOutputStream( fileOutputStream );
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter( dataOutputStream ));
            
            for ( Part part : worker.getParts() )
            {
                for ( Vertex vertex : part.getVertices() )
                {
                    bufferedWriter.write( vertex.output() );
                    bufferedWriter.newLine();
                }
            }
            
            // close Worker output file
            bufferedWriter.close();
            dataOutputStream.close();
            fileOutputStream.close();
        }
        catch ( Exception exception )
        {
            err.println( "SimpleFileWorkerWriter.write: Error: " + exception.getMessage());
            exit( 1 );
        }
    }
}
